package com.glsc.ngateway.common.api.oaflow.resp;

import cn.hutool.core.net.NetUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ResponseLoginDto implements Serializable {

    private String clusterSessionIDCookieName;
    private String sessionId;
    private String languageidweaver;
    private String loginidweaver;
    private String loginuuids;
    private String token;

    public static ResponseLoginDto convertFromHttpCookie(List<String> cookies) {
        ResponseLoginDto resp = new ResponseLoginDto();
        if (!CollectionUtils.isEmpty(cookies)) {
            cookies.stream()
                    .map(NetUtil::parseCookies)
                    .flatMap(Collection::stream)
                    .forEach(cookie -> {
                        String value = cookie.getValue();
                        switch (cookie.getName()){
                            case "__clusterSessionIDCookieName":
                                resp.setClusterSessionIDCookieName(value);
                                break;
                            case "ecology_JSessionid":
                            case "JSESSIONID":
                                resp.setSessionId(value);
                                break;
                            case "languageidweaver":
                                resp.setLanguageidweaver(value);
                                break;
                            case "loginidweaver":
                                resp.setLoginidweaver(value);
                                break;
                            case "loginuuids":
                                    resp.setLoginuuids(value);
                                break;
                            default:

                        }
                    });
        }
        return resp;
    }
}
